import { createStore } from 'redux'

const initState = {
    addressList: []
}
function reducer(state = initState, action) {
    // 添加
    // 编辑
    // 删除
    switch (action.type) {
        case "addAddress":
            state.addressList.push(action.data)
            console.log(state)
            break;
        case "editAddress":
            state.addressList.forEach((item, index) => {
                if (item.id === action.data.id) {
                    state.addressList[index] = action.data
                }
            })
            break;
        case "delAddress":
            const ind = state.addressList.findIndex(item => item.id == action.data.id)
            state.addressList.splice(ind, 1)
            break;
        case "setAddressStatus":
            state.addressList.forEach((item, index) => {
                state.addressList[index].isInit = false;
                if (item.id == action.data.id) {
                    state.addressList[index].isInit = true;
                }
            })
            break;
        default: ;
    }
    return JSON.parse(JSON.stringify(state))
}


const store = createStore(reducer)

export default store